<?php

namespace Modules\Api\Http\Controllers;

use HiCommon\Service\CommonService;
use HiCommon\Service\LoginUserService;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;

class ApiController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        dd('api');
        return view('api::index');
    }

    /**
     * 发送手机验证码接口
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function send_phone_code()
    {
        try {
            $s_phone = str_trim(Request::input('phone',''));//发送验证码手机号
            return CommonService::api_send_phonecode($s_phone);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 手机验证码登录
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function phonecode_login_in()
    {
        try {
            $s_phone = Request::input('phone', '');//手机号
            $s_code = Request::input('code', '');//验证码
            $p_user = Request::input('p_user', '');//用于分享父级
            return LoginUserService::app_phonecode_login_or_register($s_phone, $s_code, $p_user);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    public function phone_login_in(){
        try {
            $s_phone = Request::input('phone','');//手机号
            $s_password = Request::input('password','');//密码
            return LoginUserService::app_login_in($s_phone,$s_password);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 单图片上传
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function single_img_upload(){
        $res = upload_single_file_by_binarystream($_FILES['file']);
        return $res;
    }

    /**
     * 多图片上传
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function multiple_imgs_upload(){
        try {
            $arr_base64_file = Request::input('file');
            $res = upload_multiple_files_by_base64($arr_base64_file);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

//    /**
//     * Show the form for creating a new resource.
//     * @return Response
//     */
//    public function create()
//    {
//        return view('api::create');
//    }
//
//    /**
//     * Store a newly created resource in storage.
//     * @param  Request $request
//     * @return Response
//     */
//    public function store(Request $request)
//    {
//    }
//
//    /**
//     * Show the specified resource.
//     * @return Response
//     */
//    public function show()
//    {
//        return view('api::show');
//    }
//
//    /**
//     * Show the form for editing the specified resource.
//     * @return Response
//     */
//    public function edit()
//    {
//        return view('api::edit');
//    }
//
//    /**
//     * Update the specified resource in storage.
//     * @param  Request $request
//     * @return Response
//     */
//    public function update(Request $request)
//    {
//    }
//
//    /**
//     * Remove the specified resource from storage.
//     * @return Response
//     */
//    public function destroy()
//    {
//    }
//
//
//
//
//
//
//
//    /**
//     * 测试 接口获取用户信息
//     *
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function api_me(){
//        return response()->json($this->guard()->user());
//
//    }
//
//    public function login(Request $request){
//        $credentials = $request->only('username', 'password');
//
//        $token = $this->guard()->attempt($credentials);
//        if ($token) {
//            return $this->respondWithToken($token);
//        }
//
//        return response()->json(['error' => 'Unauthorized'], 401);
//        //        dd(\Illuminate\Support\Facades\Request::all());
//    }
//
//    /**
//     * Get the authenticated User
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function me()
//    {
//        return response()->json($this->guard()->user());
//    }
//
//    /**
//     * Log the user out (Invalidate the token)
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function logout()
//    {
//        $this->guard()->logout();
//
//        return response()->json(['message' => 'Successfully logged out']);
//    }
//
//    /**
//     * Refresh a token.
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function refresh()
//    {
//        return $this->respondWithToken($this->guard()->refresh());
//    }
//
//    /**
//     * Get the token array structure.
//     *
//     * @param  string $token
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    protected function respondWithToken($token)
//    {
//        return response()->json([
//          'access_token' => $token,
//          'token_type' => 'bearer',
//          'expires_in' => $this->guard()->factory()->getTTL() * 60
//        ]);
//    }
//
//    /**
//     * Get the guard to be used during authentication.
//     *
//     * @return \Illuminate\Contracts\Auth\Guard
//     */
//    public function guard()
//    {
//        return Auth::guard('api');
//    }
}
